package cn.u313.service;

import cn.u313.comm.conststring.TxtzhuFU;
import cn.u313.comm.eunm.BlessingConst;
import cn.u313.comm.eunm.Type;
import cn.u313.comm.utils.ResultBlessing;
import cn.u313.comm.utils.TxtUtil;
import cn.u313.comm.utils.Util;
import cn.u313.config.BotConfig;
import cn.u313.config.NewYearConfig;
import cn.u313.entry.BlessingEntry;
import cn.u313.entry.JiLuEntry;
import cn.u313.entry.UserEntry;
import cn.u313.model.MsgInfo;
import cn.u313.repository.BlessingEntryRepository;
import cn.u313.repository.JiLuEntryRepository;
import cn.u313.repository.UserEntryRepository;
import me.kuku.utils.DateTimeFormatterUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import static cn.u313.comm.utils.Util.getTime;
import static cn.u313.comm.utils.Util.icon;

@Service
public class BlessingEntryService {
    private int price = 20;
    private int max = 7;

    @Autowired
    BlessingEntryRepository blessingEntryRepository;
    @Autowired
    UserEntryService userEntryService;
    @Autowired
    JiLuEntryRepository jiLuEntryRepository;
    @Autowired
    UserEntryRepository userEntryRepository;


    public String getCard(String qq) {
        blessingEntryRepository.byQq(qq);
        return "";
    }

    public String addCard(String group, String qq) {
        UserEntry userEntry = userEntryService.getUser(qq);
        if(userEntry==null)
            return "请先发送签到获取悦币！";

        if(userEntry.getM()<price)
            return "您的悦币不足 [ "+price+" ] 哦！";

        int number = jiLuEntryRepository.countByqqAndType(qq,Type.抽福卡);
        if(number>=max){
            return "每天只能抽"+max+ "次福卡哦！";
        }


        BlessingEntry blessingEntry = blessingEntryRepository.byQq(qq);
        //第一次
        if(blessingEntry==null){
            blessingEntry = new BlessingEntry().build(qq,group);
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(price);
            jiLuEntry.setQ0n(userEntry.getM());
            jiLuEntry.setQq0(qq);
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.抽福卡);
            jiLuEntry.setGroupNumber(group);
            jiLuEntry.setMsg(blessingEntry.get_this()+"");
            jiLuEntryRepository.save(jiLuEntry);
            blessingEntryRepository.save(blessingEntry);
        }else{
            blessingEntry.card(NewYearConfig.probability);
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(price);
            jiLuEntry.setQ0n(userEntry.getM());
            jiLuEntry.setQq0(qq);
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.抽福卡);
            jiLuEntry.setGroupNumber(group);
            jiLuEntry.setMsg(blessingEntry.get_this()+"");

            jiLuEntryRepository.save(jiLuEntry);
            blessingEntryRepository.save(blessingEntry);
        }
        //userEntry.payMe(-price);
        userEntry.payMe(-price);
        userEntryRepository.save(userEntry);

        return blessingEntry.getMsg();
    }
    public String select(String qq){

        BlessingEntry blessingEntry = blessingEntryRepository.byQq(qq);
        if(blessingEntry==null){
            blessingEntry = new BlessingEntry();
        }
        return blessingEntry.getMsg(0);
    }

    public String new30(String qq) throws UnsupportedEncodingException {
//        if(true)return "[CAT:face,id=137]暂时还不能开奖哦，先看看春晚吧，还能抢15亿红包哦 戳》 u.jd.com/PKNB2Fk";
        UserEntry userEntry = userEntryService.getUser(qq);
        if (userEntry == null) {
            return  "用户不存在";
        }


            BlessingEntry blessing=blessingEntryRepository.byQq(qq);
            if(blessing==null){
                //第一次抽
                return "未集齐一套兔卡哦！小嘤祝您新春大吉！"+ TxtzhuFU.新年祝福语();
            }else{
                // blessing.card(probability);
                //session.update(blessing);
                if(blessing.getWEi()&&blessing.getPrize()==0){
                    //集齐
//                    msgs="已集齐福卡哦！小嘤祝您新春大吉！";
                    ResultBlessing b=blessing.getJiiang(jiLuEntryRepository.countByqqAndCard(qq));
                    String msgs=b.toString();
//                    u0.setM(u0.getM()+b.总数);
//                    u0.setHongBao(u0.getHongBao()+b.红包);
                    blessing.setPrize(1);
                    blessing.setPrizeyb(b.总数+"");
                    blessing.setHb(b.红包+"");
                    blessing.setTime1(Util.getTime());


                    JiLuEntry jiLuEntry = new JiLuEntry();
                    jiLuEntry.setN(b.总数);
                    jiLuEntry.setQ0n(userEntry.getM());
                    jiLuEntry.setQq0(qq);
                    jiLuEntry.setTime(Util.getTime());
                    jiLuEntry.setType(Type.瓜分大奖);
//                    jiLuEntry.setGroupNumber(group);
                    jiLuEntry.setMsg(URLEncoder.encode(msgs,"UTF-8"));

                    blessingEntryRepository.save(blessing);
                    userEntry.payMe(b.总数);
                    userEntry.setHongBao(b.红包+userEntry.getHongBao());
                    userEntryRepository.save(userEntry);
                    System.out.println(userEntry.getM());
                    jiLuEntryRepository.save(jiLuEntry);
                    System.out.println(blessing.doc());
                    System.out.println(b.test());


                    return msgs;
//
                }else if(blessing.getPrize()==1){
                    JiLuEntry jiLuEntry = jiLuEntryRepository.countByqqAndTypeEntry(qq,Type.瓜分大奖);
                    return "[CAT:face,id=137]你已经领过奖了哦！\n"+ URLDecoder.decode(jiLuEntry.getMsg(),"UTF-8");
                }else {
                    return "未集齐一套兔卡哦！小嘤祝您新春大吉！"+ TxtzhuFU.新年祝福语();
                }
            }

        }

    public String zengSong(String qq,String qq1,String group,String type) {
            //收卡QQ

            int cardId=0;
            try {

                if(type==null){
                    return ("请在【】里填写需要转赠的兔卡序号\n" +
                            "对应规则如下\n" +
                           icon+ "健康兔:0\n" +
                           icon+ "幸福兔:1\n" +
                           icon+ "晋升兔:2\n" +
                           icon+ "招财兔:3\n" +
                           icon+ "平安兔:4\n" +
                           icon+ "好运兔:5\n" +
                           icon+ "万能卡:6");
                }
                if(qq1==null)return ("请艾特要转赠的对象");


                cardId=new Integer(type);
                if(cardId>6||cardId<0){
                    throw new NullPointerException("兔卡序号范围请在 0 - 6 ");
                }

                BlessingEntry blessing=blessingEntryRepository.byQq(qq);
                if(blessing==null){
                    return "你还未获得兔卡呢";
                }else{

                    BlessingEntry blessing1=blessingEntryRepository.byQq(qq1);

                    if(blessing1==null)

                        return "对方暂未参与此次活动哦！";

                    //扣赠送方卡
                    String zt=blessing.zsCard(cardId);
                    if(zt.equals("转赠成功")){
                        //给赠送方卡
                        blessing1.addCard(cardId);

                        JiLuEntry jiLuEntry = new JiLuEntry();
                        jiLuEntry.setN(0);
                        jiLuEntry.setQ0n(0);
                        jiLuEntry.setQq0(qq);
                        jiLuEntry.setTime(Util.getTime());
                        jiLuEntry.setType(Type.转赠福卡);
                        jiLuEntry.setGroupNumber(group);
                        jiLuEntry.setMsg(BlessingConst.getConst(cardId));
                        jiLuEntryRepository.save(jiLuEntry);
                        blessingEntryRepository.save(blessing);
                        blessingEntryRepository.save(blessing1);
                        return "成功转赠一张 ["+ BlessingConst.getConst(cardId)+"] 给"+ qq1;

                    }else{
                        return "转赠失败 ["+ BlessingConst.getConst(cardId)+"]卡的数量不足";
                    }


                }
            } catch (Exception e) {
                return "转赠失败 失败原因\n ["+e.getMessage()+"]";
            }


        }


    public String duiHuan(String qq, String group, String type) {

            if(type==null||type.equals("")){
                return ("请在【】里填写需要兑换的兔卡序号\n" +
                        "对应规则如下\n" +
                        icon+"健康兔:0\n" +
                        icon+"幸福兔:1\n" +
                        icon+"晋升兔:2\n" +
                        icon+"招财兔:3\n" +
                        icon+"平安兔:4\n" +
                        icon+"好运兔:5\n" +
                        "如：万能卡兑换【1】");
            }
            int cardId=new Integer(type);
            if(cardId>6||cardId<0){
                return ("兔卡序号范围请在 0 - 6 ");
            }

            BlessingEntry blessing= blessingEntryRepository.byQq(qq);
            if(blessing==null)
                return "你还未获得万能卡呢";
            if( blessing.getUniversal()<1){
                return "你还未获得万能卡呢";
            }else{
                blessing.addCard(cardId);
                blessing.jianUn();
                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(0);
                jiLuEntry.setQ0n(0);
                jiLuEntry.setQq0(qq);
                jiLuEntry.setTime(Util.getTime());
                jiLuEntry.setType(Type.万能卡兑换);
                jiLuEntry.setGroupNumber(group);
                jiLuEntry.setMsg(BlessingConst.getConst(cardId));
                jiLuEntryRepository.save(jiLuEntry);
                blessingEntryRepository.save(blessing);
                return  icon+"成功用万能卡兑换 ["+ BlessingConst.getConst(cardId)+"] \n"+blessing.getMsg(0);
            }

    }

    public String lingQuTuanYuanCard(String qq, String group) {
        int numberQiandao = jiLuEntryRepository.countByqqAndTypeLike(qq,Type.领取万能卡);
        if(numberQiandao!=0){
            return "你已经领取过万能卡了哦！";
        }

        BlessingEntry blessingEntry= blessingEntryRepository.byQq(qq);
        if(blessingEntry==null){
            blessingEntry = new BlessingEntry().build(qq,group);
            blessingEntry.addCard(BlessingConst.万能卡);
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(price);
            jiLuEntry.setQ0n(0);
            jiLuEntry.setQq0(qq);
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.领取万能卡);
            jiLuEntry.setGroupNumber(group);
            jiLuEntry.setMsg(blessingEntry.get_this()+"");
            jiLuEntryRepository.save(jiLuEntry);
            blessingEntryRepository.save(blessingEntry);
        }else{
            blessingEntry.card(NewYearConfig.probability);
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(price);
            jiLuEntry.setQ0n(0);
            jiLuEntry.setQq0(qq);
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.领取万能卡);
            jiLuEntry.setGroupNumber(group);
            jiLuEntry.setMsg(blessingEntry.get_this()+"");
            blessingEntry.addCard(BlessingConst.万能卡);
            jiLuEntryRepository.save(jiLuEntry);
            blessingEntryRepository.save(blessingEntry);
        }
        return "领取万能卡成功！"+(TxtzhuFU.除夕祝福语());
    }

    public String new30Thread(String qq, MsgInfo msgInfo) throws UnsupportedEncodingException {
         msgInfo.sendMsg("[CAT:face,id=137]用户 [ "+qq+" ]\n"+new30(qq));
         return "";
    }
}

